package com.loe.mvp

import android.content.res.Resources
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.view.MotionEvent
import com.loe.mvp.ext_app.setStatusText
import com.loe.mvp.ext_java.safe
import com.loe.mvp.initer.BaseIniter
import com.loe.mvp.initer.BaseLoading
import com.loe.mvp.initer.BaseToast
import com.loe.mvp.util.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

abstract class BaseActivity : AppCompatActivity(), BaseView
{
    lateinit var activity: BaseActivity
    private lateinit var mToast: BaseToast
    private lateinit var mLoading: BaseLoading

    protected var isInit = false
    protected var isShow = false
    private var lastResumeTime = 0L
    protected var delayResumeDuration = 5_000

    override fun onCreate(savedInstanceState: Bundle?)
    {
        BaseIniter.onBaseView?.onCreatePre(this)
        super.onCreate(savedInstanceState)
        activity = this
        mToast = BaseToast(activity)
        mLoading = BaseLoading(activity)

        setStatusText()

        BaseIniter.onBaseView?.onCreate(this)

        runCatching { EventBus.getDefault().register(this) }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onBusMessage(message: BusMessage)
    {
        if (message.path == path)
        {
            when (message.msg)
            {
                // 处理所有页面的关闭消息
                CLOSE_PAGE -> finish()
                // 处理所有页面的更新消息
                UPDATE_PAGE -> loadData()
            }
            BaseIniter.onBaseView?.onBus(this, message.msg)
            onBus(message.msg)
        } else if (message.path == "")
        {
            BaseIniter.onBaseView?.onBus(this, message.msg)
            onBus(message.msg)
        }
    }

    override val root: BaseActivity get() = activity
    override val toast: BaseToast get() = mToast
    override val loading: BaseLoading get() = mLoading

    override fun onResume()
    {
        super.onResume()
        isShow = true
        if(System.currentTimeMillis() - lastResumeTime > delayResumeDuration)
        {
            onDelayResume()
        }
        BaseIniter.onBaseView?.onResume(this)
        if (!isInit)
        {
            onInit()
            isInit = true
        }
        lastResumeTime = System.currentTimeMillis()
    }

    override fun onPause()
    {
        isShow = false
        super.onPause()
    }

    protected open fun onDelayResume()
    {
    }

    protected open fun onInit()
    {
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean
    {
        if (ev != null) safe { BaseIniter.onBaseView?.onTouchEvent(ev) }
        return super.dispatchTouchEvent(ev)
    }

    override fun onDestroy()
    {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
        BaseIniter.onBaseView?.onDestroy(this)
    }

    private var mDpiResources: DpiResources? = null

    override fun getResources(): Resources
    {
        return if (DpiResources.getDpi() > 0)
        {
            if (mDpiResources == null)
            {
                mDpiResources = DpiResources(super.getResources())
            }
            mDpiResources!!
        } else
        {
            super.getResources()
        }
    }
}